package com.zyh.config;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class MyAspect {
    @Pointcut("execution(* com.zyh.service.impl.*.*(..))")
    public void pointCut(){}

    @Before("pointCut()")
    public void before(JoinPoint joinPoint){
//        Signature signature = joinPoint.getSignature();//目标方法
//        Object target = joinPoint.getTarget();//目标对象
//        Object proxy = joinPoint.getThis();//代理对象
//        Object[] args = joinPoint.getArgs();//方法实参
//        System.out.println("signature = " + signature);
//        System.out.println("target = " + target.getClass());
//        System.out.println("proxy = " + proxy.getClass());
//        System.out.println("args = " + args);
//        System.out.println("前置增强");
    }

    @After("pointCut()")
    public void after(JoinPoint joinPoint){
//        System.out.println("后置增强");
    }

    @Around("pointCut()")
    public Object around(ProceedingJoinPoint joinPoint) {
        Object result = null;
        try {
            System.out.println("环绕开始");
            result = joinPoint.proceed();
            System.out.println("环绕结束");
        }catch (Throwable t){
            t.printStackTrace();
            throw new RuntimeException(t);
        }
        return result;
    }
}
